﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Diagnostics;
using EnvDTE;
using DoxygenBrowserAddin.Utils;
using DoxygenBrowser.Common.Model;

namespace DoxygenBrowserAddin.Data
{
  public class TokenDictionary
  {
    private const string CONST_CLASS_MEMBER = "clm";
    private const string CONST_CLASS = "cl";
    private const string CONST_FILE = "file";

    private int _iNumEntries;

    private Dictionary<string, FileLocation> _dictionaryFileLoc;

    /// <summary>
    /// Initializes a new instance of the <see cref="TokenDictionary"/> class.
    /// </summary>
    public TokenDictionary()
    {
      _dictionaryFileLoc = new Dictionary<string, FileLocation>();
    }

    /// <summary>
    /// Gets the num entries.
    /// </summary>
    /// <value>The num entries.</value>
    public int NumEntries
    {
      get { return _iNumEntries; }
    }

    public void AddTokenEntry(string strTokenKey, FileLocation fileLocation)
    {

      if (!string.IsNullOrEmpty(strTokenKey) &&
        (!_dictionaryFileLoc.ContainsKey(strTokenKey)))
      {
        _dictionaryFileLoc.Add(strTokenKey, fileLocation);
      }
      else
      {

      }

      _iNumEntries++;
    }

    /// <summary>
    /// Finds the specified code location.
    /// </summary>
    /// <param name="codeLocation">The code location.</param>
    /// <returns></returns>
    public FileLocation Find(CodeLocation codeLocation)
    {
      FileLocation result = null;


      string key = string.Empty;
      string member = string.Empty;

      if (codeLocation.CodeScope.ContainsKey(vsCMElement.vsCMElementFunction))
      {
        member = codeLocation.CodeScope[vsCMElement.vsCMElementFunction];
      }
      else if (codeLocation.CodeScope.ContainsKey(vsCMElement.vsCMElementEvent))
      {
        member = codeLocation.CodeScope[vsCMElement.vsCMElementEvent];
      }
      else if (codeLocation.CodeScope.ContainsKey(vsCMElement.vsCMElementVariable))
      {
        member = codeLocation.CodeScope[vsCMElement.vsCMElementVariable];
      }
      else if (codeLocation.CodeScope.ContainsKey(vsCMElement.vsCMElementClass))
      {
        member = codeLocation.CodeScope[vsCMElement.vsCMElementClass];
      }
      else if (codeLocation.CodeScope.ContainsKey(vsCMElement.vsCMElementNamespace))
      {
        member = codeLocation.CodeScope[vsCMElement.vsCMElementNamespace];
      }

      key = Scope.Normalizate(member);

      if (_dictionaryFileLoc.ContainsKey(key))
      {
        result = _dictionaryFileLoc[key];
      }

      return result;
    }

  }
}
